*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*	Summary statistics for the OLS VAM (all students) and lottery samples.
*	----------------------------------------------------------------------------
*	IN: 	[analysis file]
*
*	OUT:	//name of output
			local outname 		"${city}_descriptives.csv"
			//name of sheet in deck
			local sheetname 	"${city}_descriptives"
*	----------------------------------------------------------------------------

	args sample sch_res ptype bw
	tokenize `sample', parse("_")
	local years "`5'`6'`7'`8'"

*	settings

	local covs bl_hispanic bl_black bl_white bl_asian bl_female bl_fr_lunch bl_sped bl_ell bl_math bl_ela screened unscreened ///
		noncompliant nooffer


*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

	cap log close
	log using "${log}`sheetname'.smcl", replace

*	load

	use "${builddata}${city}_analysisfile_vam`sample'`sch_res'`bw'.dta", clear
	if inlist("${city}","NYC","NYCms"){
		merge m:1 sch year using "${builddata}${city}_schoolcovs", nogen keep(1 3)
		gen unscreened = !screened
	}
	else if inlist("${city}","DEN"){
		gen screened   = 0
		gen unscreened = 1
	}

*	summarize

	//get noncompliance rates
	cap drop offersum
	egen offersum = rowtotal(offer_*)
	g nooffer = 1 - offersum
	g noncompliant = 0
	qui levelsof sch, local(allschools)
	foreach sch in `allschools' {
		cap confirm var offer_`sch'
		if _rc == 0 replace noncompliant = 1 if enr_`sch' == 0 & offer_`sch' == 1
	}
	replace noncompliant = 1 if offersum == 0

	//demean scores
	foreach x in math ela {
		qui su bl_`x'
		replace bl_`x' = bl_`x' - r(mean)
	}

	//OLS sample
	eststo ols: qui estpost summarize `covs'
	unique sch
	estadd scalar J = r(sum)

	//lottery sample
	ivset, ptype(`ptype')
		local S = r(S)
	eststo lottery_`ptype': qui estpost summarize `covs' if `S'

	unique sch if `S' == 1
	estadd scalar J = r(sum)
	unique sch if lottery_`ptype'_sch
	estadd scalar L = r(sum)

*	output

	esttab2 using "${tables}/raw/`outname'", cells("mean(fmt(j))") mtitles ///
		nonumbers replace scalars(N J L)
	estimates clear

	insheet using "${tables}/raw/`outname'", clear nonames
	export excel using "${tables}${city}_tabs_`c(username)'_`c(current_date)'.xlsx", sheetreplace sheet("`sheetname'")

	log close
